-- =============================================
-- Author:		Wout Breugelmans (& Jeff Moden)
-- Create date: 16/02/2010
-- Description:	Houdt modify_date van Klanten juist.
-- =============================================
CREATE TRIGGER dbo.tg_UpdateKlanten 
   ON  dbo.Klanten 
   AFTER UPDATE
AS
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
IF NOT UPDATE (modify_date)
BEGIN
--===== Update the "modified date" column but only if an actual change
	--- of data has occurred somewhere in the row.  
	--- Because BINARY_CHECKSUM was used, it will detect changes in 
	--- case and accent markings, as well.
 UPDATE dbo.Klanten 
    SET modify_date = GETDATE()
   FROM (SELECT klantID, BINARY_CHECKSUM(*) AS MyCheck FROM Deleted) d
  INNER JOIN 
        (SELECT klantID, BINARY_CHECKSUM(*) AS MyCheck FROM Inserted) i
     ON d.klantID = i.klantID
    AND d.MyCheck <> i.MyCheck
  INNER JOIN dbo.Klanten u
     ON d.klantID = u.klantID
END
GO
